/*
 * Copyright (c) 2015, the Dart project authors.
 *
 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package vm.element

// This file is generated by the script: pkg/vm_service/tool/generate.dart in dart-lang/sdk.

import com.google.gson.JsonObject

/**
 * {@link Message} 提供有关挂起的 isolate 消息以及将被调用以处理它的函数的信息。
 */
class Message(json: JsonObject) : Response(json) {

    /**
     * 对将被调用以处理此消息的函数的引用。
     *
     * 可以返回 <code>null</code>。
     */
    fun getHandler(): FuncRef? {
        val obj = json.get("handler")?.asJsonObject ?: return null
        val type = json.get("type").asString
        if ("Instance" == type || "@Instance" == type) {
            val kind = json.get("kind").asString
            if ("Null" == kind) return null
        }
        return FuncRef(obj)
    }

    /**
     * isolate 消息队列中的索引。第 0 个消息是下一个要处理的消息。
     */
    fun getIndex(): Int {
        return getAsInt("index")
    }

    /**
     * 处理程序的源位置。
     *
     * 可以返回 <code>null</code>。
     */
    fun getLocation(): SourceLocation? {
        val obj = json.get("location")?.asJsonObject ?: return null
        val type = json.get("type").asString
        if ("Instance" == type || "@Instance" == type) {
            val kind = json.get("kind").asString
            if ("Null" == kind) return null
        }
        return SourceLocation(obj)
    }

    /**
     * 已解码消息的实例 ID。此 ID 可以传递给其他 RPC，例如，
     * getObject 或 evaluate。
     */
    fun getMessageObjectId(): String? {
        return getAsString("messageObjectId")
    }

    /**
     * 描述此消息的建议名称。
     */
    fun getName(): String? {
        return getAsString("name")
    }

    /**
     * 编码消息的大小（字节）。
     */
    fun getSize(): Int {
        return getAsInt("size")
    }
}
